//
//  NSString+DHString.m
//  新浪微博OC
//
//  Created by DIANGHONG on 2018/4/4.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "NSString+DHString.h"

@implementation NSString (DHString)

/**
 拼接NSDocumentDirectory路径

 @return NSString（拼接好的路径）
 */
- (NSString *)dh_appendDocumentDir{
    
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0];

    return [docPath stringByAppendingPathComponent:self];
}

/**
 正则表达式找"<a href="http://weibo.com" rel="nofollow">新浪微博</a>"
 url和最后的新浪微博字段
 
 @param urlString 指针地址的url字符串
 @return 新浪微博字符串
 */
- (NSString *)dh_hrefUrlWithUrlString:(NSMutableString **)urlString{
    
    //"<a href="http://weibo.com" rel="nofollow">新浪微博</a>",
    
    NSString *pattern = @"<a href=\"(.*?)\" .*?\">(.*?)</a>";
    
    NSRegularExpression *regux = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
    
    NSTextCheckingResult *matchs = [regux firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
    
    NSString *text = nil;
    
    for (int i = 0; i < matchs.numberOfRanges; i++) {
        
        if (i == 0) {
            continue;
        }
        
        NSRange r = [matchs rangeAtIndex:i];
        
        NSString *subStr = [self substringWithRange:r];
        
        
        if (i != (matchs.numberOfRanges - 1)) {
            
            NSString *urlstr = *urlString;
            
            urlstr = subStr;


        }else{
            
            
            text = [subStr copy];
            
            return subStr;

        }
    }
    return nil;
}

// urlString下载image
- (void)dh_stringTodownloaderImageWithCompletion:(void(^)(UIImage *image))completion{
    
    NSURL *url = [NSURL URLWithString:self];
    
    [SDWebImageDownloader.sharedDownloader downloadImageWithURL:url options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        
        if (image) {
            completion(image);
        }
        
    }];
    
}

@end
